package com.payment.ktb.activity.main1;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/8/28.
 */
public class VIPInsuranceActivity extends BaseActivity {



    String vipFLg = "";

    @BindView(R.id.btn_vipinsurance_obtain)
    Button btn_vipinsurance_obtain;
    @BindView(R.id.tv_vipinsurance_vipDetail)
    TextView tv_vipinsurance_vipDetail;
    @BindView(R.id.tv_vipinsurance_vipTitle)
    TextView tv_vipinsurance_vipTitle;
    @BindView(R.id.cb_vipinsuranceactivity_aggress)
    CheckBox cb_vipinsuranceactivity_aggress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_vipinsurance);
        ButterKnife.bind(this);
        initTitle("权益详情");
        getVIPInsuranceInfo();

    }



    @OnClick({R.id.btn_vipinsurance_obtain,R.id.tv_vipinsuranceactivity_protocol})
    public void clickEvent(View view) {


        switch (view.getId()) {


            //服务协议
            case R.id.tv_vipinsuranceactivity_protocol:
                    startActivity(VipProtocalServiceActivity.class);
                break;

            //申请会员
            case R.id.btn_vipinsurance_obtain:
                if (!cb_vipinsuranceactivity_aggress.isChecked()) {
                    ToastUtils.showToast("请先同意服务协议！");
                } else if (ConstantsUser.vipFlgCodeVIP.equals(vipFLg)) {
                    obtainInsurance();
                } else {
                    AlertDialogUtils.showAlertDialog(mContext, "需要先成为会员，才能领取", "取消", "注册会员", new AlertInterface() {
                        @Override
                        public void confirm(Dialog alertDialog) {
                            startActivity(EnrollVIPActivity.class);
                            alertDialog.dismiss();
                        }
                    });

                }

                break;
        }



    }



    private void obtainInsurance() {

        progressDialogUtils.showProgress();

        //构造传输参数
        String vipCode = ConstantsUser.vipCodeSafe;
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5（picNbr+key）
        String sign = Md5Utils.md5Hex(vipCode + SharedPreferencesUtils.getString(ConstantsUser.userKey));



        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("vipCode",vipCode);
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopVipDrawState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {


                        btn_vipinsurance_obtain.setText("领取处理中");
                        btn_vipinsurance_obtain.setEnabled(false);

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });
    }


    /**
     * 获取保险信息
     */
    public void getVIPInsuranceInfo() {

        progressDialogUtils.showProgress();

        //构造传输参数
        String vipCode = ConstantsUser.vipCodeSafe;
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5（picNbr+key）
        String sign = Md5Utils.md5Hex(vipCode + SharedPreferencesUtils.getString(ConstantsUser.userKey));



        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();

        map.put("vipCode",vipCode);
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopVipInfoState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {


                    tv_vipinsurance_vipTitle.setText(jsonObject.optString("vipTitle"));
                    tv_vipinsurance_vipDetail.setText(jsonObject.optString("vipDetail"));
                        vipFLg = jsonObject.optString("vipFLg");
                        String drawFlg = jsonObject.optString("drawFlg");
                        if (ConstantsUser.vipDrawFlg1.equals(drawFlg)) {
                            btn_vipinsurance_obtain.setText("领取处理中");
                            btn_vipinsurance_obtain.setEnabled(false);
                        } else if (ConstantsUser.vipDrawFlg2.equals(drawFlg)) {
                            btn_vipinsurance_obtain.setText("已领取");
                            btn_vipinsurance_obtain.setEnabled(false);
                        } else {
                            btn_vipinsurance_obtain.setText("未领取");
                            btn_vipinsurance_obtain.setEnabled(true);
                        }

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });

    }
}
